package top.yifengxiner.blockchain.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.yifengxiner.blockchain.pojo.entity.Academic;
import top.yifengxiner.blockchain.pojo.entity.Course;
import top.yifengxiner.blockchain.pojo.entity.Diploma;
import top.yifengxiner.blockchain.pojo.entity.Educated;
import top.yifengxiner.blockchain.pojo.vo.CertificateVo;
import top.yifengxiner.blockchain.pojo.vo.IdVo;
import top.yifengxiner.blockchain.pojo.vo.StringVo;
import top.yifengxiner.blockchain.service.*;
import top.yifengxiner.common.core.pojo.AjaxResult;
import top.yifengxiner.common.utils.TokenUtils;

import java.util.List;

/**
 * @author ：baixiao
 * @date ：Created in 2021/5/11 18:55
 * @description：教育者
 * @modified By：
 * @version: 0.0.1$
 */
@Api("教育者模块")
@RestController
@RequestMapping("/blockchain")
public class EducatorController
{
    @Autowired
    private EducatedService educatedService;

    @Autowired
    private AcademicService academicService;

    @Autowired
    private DiplomaService diplomaService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private EducatorService educatorService;

    /**
     * 获得受教育者列表
     * @return
     */
    @ApiOperation("通过教育者ID获得教育者列表")
    @PostMapping("/educator/get_educated")
    public AjaxResult getEducated(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        List<Educated> list =educatedService.getListById(idVo.getId());
        return AjaxResult.success(list);
    }

    /**
     * 通过受教育者ID获得受教育者的信息
     * @param idVo
     * @return
     */
    @ApiOperation("通过受教育者Id获取受教育者的信息")
    @PostMapping("/educator/get_educated_info")
    public AjaxResult getEducatedInfo(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        Educated educated = educatedService.getEducatedById(idVo.getId());
        return AjaxResult.success(educated);
    }

    /**
     * 通过受教育者ID获得学历证书列表
     * @return
     */
    @ApiOperation("通过受教育者ID获得学历证书列表")
    @PostMapping("/educator/get_academic")
    public AjaxResult getAcademic(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        List<Academic> list = academicService.getAcademic(idVo);
        return AjaxResult.success(list);
    }

    /**
     * 通过通过学历证书ID获得证书信息
     * @return
     */
    @ApiOperation("通过学历证书ID获得学历证书信息")
    @PostMapping("/educator/get_academic_info")
    public AjaxResult getAcademicInfo(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        Academic academic = academicService.getAcademicInfo(idVo);
        return AjaxResult.success(academic);
    }


    /**
     * 通过受教育者ID获得学位证书列表
     * @return
     */
    @ApiOperation("通过受教育者ID获得学位证书列表")
    @PostMapping("/educator/get_diploma")
    public AjaxResult getDiploma(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        List<Diploma> list = diplomaService.getDiploma(idVo);
        return AjaxResult.success(list);
    }

    @ApiOperation("通过学位证书ID获得学位证书信息")
    @PostMapping("/deucator/get_diploma_info")
    public AjaxResult getDiplomaInfo(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        Diploma diploma = diplomaService.getDiplomaInfo(idVo);
        return AjaxResult.success(diploma);
    }

    /**
     * 通过学历证书ID获得课程信息
     * @return
     */
    @ApiOperation("通过学历证书ID获得课程信息")
    @PostMapping("/educator/get_course")
    public AjaxResult getCourse(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        List<Course> list = courseService.getCourse(idVo);
        return AjaxResult.success(list);
    }

    @ApiOperation("生成学历证书")
    @PostMapping("/educated/create_academic")
    public AjaxResult createAcademic(@RequestBody CertificateVo certificateVo)
    {
        academicService.insertAcademic(certificateVo);
        return AjaxResult.success();
    }

    @ApiOperation("生成学位证书")
    @PostMapping("/educator/create_diploma")
    public AjaxResult createDiploma(@RequestBody CertificateVo certificateVo)
    {
        diplomaService.createDiploma(certificateVo);
        return AjaxResult.success();
    }

    @ApiOperation("通过token获取Educator")
    @PostMapping("/educator/get_educator_token")
    public AjaxResult getEducator(@RequestBody StringVo stringVo)
    {
        String EducatorName = TokenUtils.getSubject(stringVo.getString());
        return AjaxResult.success(educatorService.getEducatorByName(EducatorName));
    }
}
